package python

import (
	"awesomeTask/application/client"
	"awesomeTask/application/models/programe"
	"awesomeTask/application/models/webhook"
	"awesomeTask/application/variable"
	awesomeTask "awesomeTask/commom"
	"awesomeTask/commom/StringHelper"
	"awesomeTask/system/http"
	"awesomeTask/system/rpc"
	"fmt"
)

type WebHookHandler struct {
	http.HttpBaseHandler
}

func (handler WebHookHandler) ActionAddHook() interface{} {
	projectName := handler.PostOne("project_name").(string)
	accssToken := StringHelper.UniqueId(32)
	runShell := handler.PostOne("run_shell").(string)
	dockerImage := handler.PostOne("docker_image").(string)
	webhook.CreateHook(0, projectName, accssToken, dockerImage, runShell)
	return map[string]interface{}{
		"msg": "ok",
	}
}
func (handler WebHookHandler) ActionEditHook() interface{} {
	id := handler.PostOne("id").(string)
	accssToken := handler.PostOne("access_token").(string)
	projectName := handler.PostOne("project_name").(string)
	runShell := handler.PostOne("run_shell").(string)
	dockerImage := handler.PostOne("docker_image").(string)
	webhook.UpdateHook(StringHelper.ParseInt(id), projectName, accssToken, dockerImage, runShell)
	return map[string]interface{}{
		"msg": "ok",
	}
}
func (handler WebHookHandler) ActionList() interface{} {
	page := handler.PostOne("page", 1).(int)
	pageSize := handler.PostOne("page_size", 20).(int)
	data := awesomeTask.Pager("select * from "+webhook.TABLE_NAME, page, pageSize)
	return map[string]interface{}{
		"data": data,
	}
}
func (handler WebHookHandler) ActionTrigger() interface{} {
	projectName := handler.Get("project_name").(string)
	accssToken := handler.Get("access_token").(string)
	hook := webhook.GetHookByProjectName(projectName)
	if hook == nil {
		return GetErrMsg(PROJECT_CAN_NOT_FIND)
	}
	if accssToken != hook["access_token"] {
		return GetErrMsg(ACCESS_TOKEN_ERR)
	}
	taskInfo := programe.GetTaskBaseInfo("7")
	fmt.Println(taskInfo)
	taskInfo["uniqueTaskId"] = StringHelper.UniqueId(10)
	inputParams := hook
	taskInfo["extendParams"] = inputParams
	res, err := rpc.RpcCall(variable.ON_TASK_REMOTE_FORK_TO_MASTER, taskInfo, client.GetServerCon(), 5000)
	return map[string]interface{}{
		"res": res,
		"err": err,
	}
}
func (handler WebHookHandler) GetModuleName() string {
	return "/hook/"
}
